<?php

namespace App\Models\Factory;

use App\Helpers\DateUtils;
use App\Helpers\Generator\TokenGenerator;
use App\Helpers\Logger\SLogger;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\Insurance;
use App\Strategies\SexStrategy;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * 保险工厂
 * Class InsuranceFactory
 * @package App\Models\Factory
 */
class InsuranceFactory extends AbsModelFactory
{
	/*
	*
	*通过用户id查询该用户保险信息
	*
	*/
	public static function getinsuByuserid($userid){
		// 联查用户表、保险产品表查出用户保险信息
        $user = Insurance::select('insurance.*', 'insurance_product.proname', 'user.nickname', 'user.userphone', 'user.birthday', 'user.sex')
            ->leftJoin('user', 'insurance.user_id', '=', 'user.id')            
        	->leftJoin('insurance_product', 'insurance.bxproid', '=', 'insurance_product.proid')
            ->where(['insurance.user_id' => $userid])
            ->first();
        //第三方保险产品未知时定义为‘请稍后’
        $user['proname'] = $user['proname'] ?: '请稍后';
        return $user;
	}
	/*
	*
	*添加用户投保信息
	*
	*/
	public static function insertinsurance($brandname,$bxproid,$user_id,$bxtypename,$bxexpire,$bxarticle,$resulturl){
		$result = Insurance::firstOrCreate(
			  	[
			  		'user_id' => $user_id
			  	],
			  	[
			  		'brandname' => $brandname,
			  		'bxproid' => $bxproid,
			  		'user_id' => $user_id,
			  		'bxtypename' => $bxtypename,
			  		'bxexpire' => $bxexpire,
			  		'bxarticle' => $bxarticle,
			  		'resulturl' => $resulturl
			  	]
		    );
		return $result;
	}
}
